﻿function MedoraDialogWindow(parent) {
    this.height = 300;
    this.width = 400;
    this.parent = parent;
    this.showButtons = false;
    this.title = "...";
    this.content;
    this.save;
    this.close;
    this.saveNotClose;
    this.load;
    

}
MedoraDialogWindow.prototype = {
    create: function (width, height, title, content) {
        var self = this;
        self.content = content;
        if (width != "") self.width = width;
        if (height != "") self.height = height;
        self.title = title;

        var clickHandlerSave = $.proxy(this.save, this);
        var clickHandlerSaveNotClose = $.proxy(this.saveNotClose, this);
        var clickHandlerClose = $.proxy(this.close, this);
        var p = self.parent;

        var strButtons = "";

        if (self.showButtons == "OkCancel" || self.showButtons == "SaveCancel" || self.showButtons == "SaveSaveCloseCancel") {
            var btn1 = "";
            var btn3 = "";
            var btn2 = "";

            switch(self.showButtons) {
                case "OkCancel":
                    btn1 = "Ok";
                    btn2 = "Cancel";
                    break;
                case "SaveCancel":
                    btn1 = "Save";
                    btn2 = "Close";
                    break;
                case "SaveSaveCloseCancel":
                    btn1 = "Save & Close";
                    btn3 = "Save";
                    btn2 = "Close";
                    break;
            }

            if (btn3.length > 0) strButtons = '<input type="button" id="btnSave" value="' + btn3 + '" class="btn btn-default saveOnlyModal"/>';
            strButtons += '<input type="button" id="btnModalXSave" value="'+btn1+'" class="btn btn-primary saveModal"/>' +
            '<input type="button" class="btn btn-default closeModal" value="'+btn2+'"/>';

            $(p).undelegate(".saveModal", "click");
            $(p).delegate(".saveModal", "click", function () {
                var isValid = clickHandlerSave();
                
                
                if (isValid == true) {
                    $(p).undelegate(".saveModal", "click");
                    clickHandlerClose();
                } else {
                    $(".validationError", p).val($(".validationError", p).attr("errorMessage"));
                }
            });
            
            $(p).undelegate(".saveOnlyModal", "click");
            $(p).delegate(".saveOnlyModal", "click", function () {
                var isValid = clickHandlerSaveNotClose();

                if (isValid == true) {
                    $(p).undelegate(".saveOnlyModal", "click");
                } else {
                    $(".validationError", p).val($(".validationError", p).attr("errorMessage"));
                }
            });
        }

        var str = '<div class="modal" id="wndModalX">' +
                    '<div class="modal-dialog" style="width: '+self.width+'px; height: '+self.height+'px">' +
                        '<div class="modal-content">' +
                            '<div class="modal-header">' +
                            '<button type="button" class="close closeModal" data-dismiss="modal" aria-hidden="true">×</button>' +
                            '<h4 class="modal-title">' + self.title + '</h4>' +
                            '</div>' +
                            '<div class="modal-body" id="contentModalX">' +
                            self.content +
                            '</div>' +
                            '<div class="modal-footer">' +
                            strButtons +
                            '</div>' +
                        '</div>' +
                    '</div>' +
            '</div>';
        

        $(p).undelegate(".closeModal", "click");
        $(p).delegate(".closeModal", "click", function () {
            clickHandlerClose();
            self = null;
        });

        $(p).undelegate(".validationError", "click");
        $(p).delegate(".validationError", "click", function () {
            $(this).val("");
            $(this).removeClass("validationError");
        });


        $(str).appendTo(p).show('slow');
    },

    createWithSave: function (width, height, title, content) {
        var self = this;
        self.showButtons = "SaveCancel";
        self.create(width, height, title, content);
    },
    createWithSaveSaveClose: function (width, height, title, content) {
        var self = this;
        self.showButtons = "SaveSaveCloseCancel";
        self.create(width, height, title, content);
    },
    createConfirm: function (width, height, title, content) {
        var self = this;
        self.showButtons = "OkCancel";
        self.create(width, height, title, content);
    },

    close: function () {
        var p = this.parent;
       // $(".closeModalXButton", p).die("click");
        $("#wndModalX", p).remove();
    },
    addContent: function (str) {
        var p = this.parent;
        $("#contentModalX", p).append(str);
    }
};

var MedoraConfirm=function (parent, header, message, func) {
    
    var content = "<div>" + message + "</div>";
    var dlg = new MedoraDialogWindow(parent);
    dlg.save = function () { func(dlg); };
    
    dlg.createConfirm(400, 70, header, content);
}